import java.util.Scanner;

public class Receptionist {
    private final Scanner scanner;
    private final Film[] films;
    private final Session[] sessions;

    public Receptionist(Film[] films, Session[] sessions) {
        this.scanner = new Scanner(System.in);
        this.films = films;
        this.sessions = sessions;
    }

    public void work() {
        while (true) {
            System.out.println("=== 前台操作菜单 ===");
            System.out.println("1-列出正在上映影片 2-列出场次信息 3-售票 4-退票 5-退出登录");
            int choice = scanner.nextInt();
            scanner.nextLine();
            switch (choice) {
                case 1:
                    listFilms();
                    break;
                case 2:
                    listSessions();
                    break;
                case 3:
                    sellTicket();
                    break;
                case 4:
                    returnTicket();
                    break;
                case 5:
                    return;
                default:
                    System.out.println("输入有误，请重新选择！");
            }
        }
    }

    private void listFilms() {
        System.out.println("=== 正在上映影片 ===");
        System.out.println("片名\t导演\t主演\t时长\t评分");
        for (Film film : films) {
            if (film != null) {
                System.out.println(film.getName() + "\t" + film.getDirector() + "\t" + film.getActor() + "\t" + film.getDuration() + "\t" + film.getScore());
            }
        }
    }

    private void listSessions() {
        System.out.println("请选择查看范围：1-当日 2-近3日 3-最近一周");
        int rangeChoice = scanner.nextInt();
        scanner.nextLine();
        String today = "2025-01-01";
        System.out.println("=== 场次信息 ===");
        System.out.println("场次 ID\t影片名\t放映时间\t放映厅\t价格");
        for (Session session : sessions) {
            if (session != null) {
                switch (rangeChoice) {
                    case 1:
                        if (session.getTime().startsWith(today)) {
                            System.out.println(session.getId() + "\t" + session.getFilmName() + "\t" + session.getTime() + "\t" + session.getHall() + "\t" + session.getPrice());
                        }
                        break;
                    case 2:
                        if (session.getTime().compareTo("2025-01-01") >= 0 && session.getTime().compareTo("2025-01-03") <= 0) {
                            System.out.println(session.getId() + "\t" + session.getFilmName() + "\t" + session.getTime() + "\t" + session.getHall() + "\t" + session.getPrice());
                        }
                        break;
                    case 3:
                        if (session.getTime().compareTo("2025-01-01") >= 0 && session.getTime().compareTo("2025-01-07") <= 0) {
                            System.out.println(session.getId() + "\t" + session.getFilmName() + "\t" + session.getTime() + "\t" + session.getHall() + "\t" + session.getPrice());
                        }
                        break;
                    default:
                        System.out.println("输入有误，默认显示最近一周场次");
                        if (session.getTime().compareTo("2025-01-01") >= 0 && session.getTime().compareTo("2025-01-07") <= 0) {
                            System.out.println(session.getId() + "\t" + session.getFilmName() + "\t" + session.getTime() + "\t" + session.getHall() + "\t" + session.getPrice());
                        }
                }
            }
        }
    }

    private void sellTicket() {
        System.out.println("请输入影片名：");
        String filmName = scanner.nextLine();
        System.out.println("请输入场次 ID：");
        String sessionId = scanner.nextLine();
        Session targetSession = null;
for (Session session : sessions) {
        if (session != null && session.getFilmName().equals(filmName) && session.getId().equals(sessionId)) {
targetSession = session;
                break;
                        }
                        }
                        if (targetSession == null) {
        System.out.println("影片或场次不存在！");
            return;
                    }

                    System.out.println("请选择座位（格式：行 列，连座用逗号分隔，如 3 5,3 6）：");
String seatInput = scanner.nextLine();
String[] seatPairs = seatInput.split(",");
boolean canSell = true;

        for (String pair : seatPairs) {
String[] coords = pair.trim().split(" ");
int row = Integer.parseInt(coords[0]) - 1;
int col = Integer.parseInt(coords[1]) - 1;
            if (row < 0 || row >= targetSession.getSeats().length
                || col < 0 || col >= targetSession.getSeats()[0].length
                || targetSession.getSeats()[row][col] == 1) {
canSell = false;
        break;
        }
        }

        if (!canSell) {
        System.out.println("所选座位不可售（已售出或超出范围），请重新选择！");
            return;
                    }

                    for (String pair : seatPairs) {
String[] coords = pair.trim().split(" ");
int row = Integer.parseInt(coords[0]) - 1;
int col = Integer.parseInt(coords[1]) - 1;
            targetSession.getSeats()[row][col] = 1;
        }

double singlePrice = Double.parseDouble(targetSession.getPrice());
double totalPrice = singlePrice * seatPairs.length;
String ticketId = "TICKET-" + System.currentTimeMillis();

        System.out.println("\n=== 购票成功 ===");
        System.out.println("电子票号：" + ticketId);
        System.out.println("影片名：" + filmName);
        System.out.println("场次 ID：" + sessionId);
        System.out.println("放映时间：" + targetSession.getTime());
        System.out.println("座位号：" + seatInput);
        System.out.println("总金额：" + totalPrice + " 元");
        System.out.println("=================\n");
    }

private void returnTicket() {
    System.out.println("请输入电子票号：");
    String ticketId = scanner.nextLine();
    boolean refundSuccess = false;

    for (Session session : sessions) {
        if (session != null) {
            for (int i = 0; i < session.getSeats().length; i++) {
                for (int j = 0; j < session.getSeats()[0].length; j++) {
                    if (session.getSeats()[i][j] == 1) {
                        session.getSeats()[i][j] = 0;
                        refundSuccess = true;
                        System.out.println("\n=== 退票成功 ===");
                        System.out.println("电子票号：" + ticketId);
                        System.out.println("释放座位：" + (i + 1) + "行" + (j + 1) + "列");
                        System.out.println("=================\n");
                        break;
                    }
                }
                if (refundSuccess) break;
            }
            if (refundSuccess) break;
        }
    }

    if (!refundSuccess) {
        System.out.println("电子票号无效或对应座位未售出，退票失败！");
    }
}
}